import { mockRequestClient as requestClient } from '#/api/request';
import type {
  CustomStatusDefinition,
  ResourceTypeCustomStatus,
} from './status-types';

enum Api {
  // 自定义状态定义管理
  CustomStatusDefinitions = '/resource-center/custom-status/definitions',
  ResourceTypeCustomStatus = '/resource-center/custom-status/resource-types/{resourceType}',

  // 资源自定义状态管理
  ResourceCustomStatus = '/resource-center/resources/{resourceId}/custom-status',

  // 状态机管理
  StateMachine = '/resource-center/state-machines',
  StateMachineValidation = '/resource-center/state-machines/{resourceType}/validate-transition',

  // 状态订阅
  StatusSubscriptions = '/resource-center/status-subscriptions',
  ResourceStatusSubscriptions = '/resource-center/resources/{resourceId}/status-subscriptions',
}

// ==================== 自定义状态定义管理 ====================

// 获取所有自定义状态定义
export function getCustomStatusDefinitions() {
  return requestClient.get<CustomStatusDefinition[]>(
    Api.CustomStatusDefinitions,
  );
}

// 创建自定义状态定义
export function createCustomStatusDefinition(
  definition: CustomStatusDefinition,
) {
  return requestClient.post(Api.CustomStatusDefinitions, definition);
}

// 更新自定义状态定义
export function updateCustomStatusDefinition(
  id: string,
  definition: Partial<CustomStatusDefinition>,
) {
  return requestClient.put(`${Api.CustomStatusDefinitions}/${id}`, definition);
}

// 删除自定义状态定义
export function deleteCustomStatusDefinition(id: string) {
  return requestClient.delete(`${Api.CustomStatusDefinitions}/${id}`);
}

// ==================== 资源类型自定义状态管理 ====================

// 获取资源类型的自定义状态配置
export function getResourceTypeCustomStatus(resourceType: string) {
  return requestClient.get<ResourceTypeCustomStatus>(
    Api.ResourceTypeCustomStatus.replace('{resourceType}', resourceType),
  );
}

// 保存资源类型的自定义状态配置
export function saveResourceTypeCustomStatus(
  resourceType: string,
  config: ResourceTypeCustomStatus,
) {
  return requestClient.put(
    Api.ResourceTypeCustomStatus.replace('{resourceType}', resourceType),
    config,
  );
}

// ==================== 资源自定义状态管理 ====================

// 更新资源自定义状态
export function updateResourceCustomStatus(
  resourceId: string,
  statusCode: string,
  reason?: string,
  metadata?: Record<string, any>,
) {
  return requestClient.put(
    Api.ResourceCustomStatus.replace('{resourceId}', resourceId),
    { statusCode, reason, metadata },
  );
}

// 获取资源自定义状态
export function getResourceCustomStatus(resourceId: string) {
  return requestClient.get(
    Api.ResourceCustomStatus.replace('{resourceId}', resourceId),
  );
}

// ==================== 状态机管理 ====================

// 验证状态转换
export function validateStatusTransition(
  resourceType: string,
  fromStatus: string,
  toStatus: string,
  eventName?: string,
) {
  return requestClient.post(
    Api.StateMachineValidation.replace('{resourceType}', resourceType),
    { fromStatus, toStatus, eventName },
  );
}

// 触发状态机事件
export function triggerStateMachineEvent(
  resourceId: string,
  eventName: string,
  payload?: Record<string, any>,
) {
  return requestClient.post(
    `/resource-center/resources/${resourceId}/state-machine/trigger`,
    { eventName, payload },
  );
}

// ==================== 状态订阅管理 ====================

// 创建状态变更订阅
export function createStatusSubscription(subscription: {
  resourceId?: string;
  resourceType?: string;
  statusTypes: string[];
  callbackUrl: string;
  events: string[];
}) {
  return requestClient.post(Api.StatusSubscriptions, subscription);
}

// 获取资源的状态订阅
export function getResourceStatusSubscriptions(resourceId: string) {
  return requestClient.get(
    Api.ResourceStatusSubscriptions.replace('{resourceId}', resourceId),
  );
}

// 删除状态订阅
export function deleteStatusSubscription(subscriptionId: string) {
  return requestClient.delete(`${Api.StatusSubscriptions}/${subscriptionId}`);
}

// ==================== 状态机可视化 ====================

// 获取状态机图
export function getStateMachineDiagram(resourceType: string) {
  return requestClient.get(
    `/resource-center/state-machines/${resourceType}/diagram`,
  );
}

// 导出状态机配置
export function exportStateMachineConfig(resourceType: string) {
  return requestClient.get(
    `/resource-center/state-machines/${resourceType}/export`,
  );
}
